<?

class validator {

	function valid_email($email) {
		if (ereg("^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[A-Za-z]{2,4})$",$email))
			return true;
		else
			return false;
	}

	function valid_alpha($text) {
		if (ereg("^([a-zA-Z ]+)$",$text))
			return true;
		else
			return false;
	}

	function valid_alphanumeric($text) {
		if (ereg("^([a-zA-Z0-9]+)$",$text))
			return true;
		else
			return false;
	}

	function valid_address($text) {
		if (ereg("^([a-zA-Z0-9 .,]+)$",$text))
			return true;
		else
			return false;
	}

	function valid_property_address($addr1,$db) {
    $sql = "SELECT addr1
            FROM properties
            WHERE addr1 = '$addr1'
              AND active_type_id = 1";
    $query = $db->query($sql);

		if($db->num_results($query)>0)
			return false;
		else
	    return true;
	}

	function valid_numeric($text) {
		if (ereg("^([0-9.]+)$",$text))
			return true;
		else
			return false;
	}
	
	function valid_state($state_abbr,$db) {
    $sql = "SELECT state
            FROM states
            WHERE state_abbr = '$state_abbr'";
    $query = $db->query($sql);

		if($db->num_results($query)>0)
			return true;
		else
	    return false;
	}

	function valid_username($username,$db) {
		$length = strlen($username);

		if($length<6 || $length>16)
			return false;

    $sql = "SELECT username
            FROM members
            WHERE username = '$username'";
    $query = $db->query($sql);

		if($db->num_results($query)>0)
	    return false;

		return true;
	}

	function valid_member_email($email,$db) {
    $sql = "SELECT email
            FROM members
            WHERE email = '$email'
              AND active_type_id = 1";
    $query = $db->query($sql);

		if($db->num_results($query)>0)
			return true;
		else
	    return false;
	}

	function valid_member($username,$db) {
    $sql = "SELECT member_id
            FROM members
            WHERE username = '$username'
              AND active_type_id = 1";
    $query = $db->query($sql);

		if($db->num_results($query)>0)
			return true;
		else
	    return false;
	}

	function valid_zip($zip,$db) {
		if (!ereg("^([0-9]{5})$",$zip))
			return false;

    $sql = "SELECT zip
            FROM zips
            WHERE zip = '$zip'";
    $query = $db->query($sql);

		if($db->num_results($query)>0)
			return true;
		else
	    return false;
	}

	function valid_phone($phone) {
		$phone = preg_replace("/[^0-9]/","",$phone);

		if (preg_match("/^1?[0-9]{10}$/",$phone))
			return true;
		else
			return false;
	}

	function valid_password($password) {
		$length = strlen($password);
		
		if($length<6)
			return false;

		$unique = strlen(count_chars($password,3));
		$difference = $unique / $length;
		
		if ($difference < .60)
			return false;

		return true;
	}
}
?>